home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / chasm4.zip / PRIMER.DOC < prev    next >
Text File  |  1984-09-13  |  49KB  |  1,783 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.                       An Assembly Language Primer
  14.  
  15.                        (C) 1983 by David Whitman
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                     TABLE OF CONTENTS
  71.  
  72.  
  73.       Introduction.......................................2
  74.  
  75.       The Computer As A Bit Pattern Manipulator..........3
  76.  
  77.       Digression: A Notation System for Bit Patterns.....5
  78.  
  79.       Addressing Memory..................................7
  80.  
  81.       The Contents of Memory: Data and Programs..........8
  82.  
  83.       The Dawn of Assembly Language......................9
  84.  
  85.       The 8088..........................................11
  86.  
  87.       Assembly Language Syntax..........................14
  88.  
  89.       The Stack.........................................17
  90.  
  91.       Software Interrupts...............................19
  92.  
  93.       Pseudo-Operations.................................21
  94.  
  95.       Tutorial..........................................23
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                                                       2
  135.  
  136.       >>INTRODUCTION<<
  137.  
  138.       Many people requesting CHASM have indicated that they are
  139.       interested in *learning* assembly language.  They are beginners,
  140.       and have little idea just where to start.  This primer is
  141.       directed to those users.  Experienced users will probably find
  142.       little here that they do not already know.
  143.  
  144.       Being a primer, this text will not teach you everything there is
  145.       to know about assembly language programming.  It's purpose is to
  146.       give you some of the vocabulary and general ideas which will help
  147.       you on your way.
  148.  
  149.       I must make a small caveat: I consider myself a relative beginner
  150.       in assembly language programming.  A big part of the reason for
  151.       writing CHASM was to try and learn this branch of programming
  152.       from the inside out.  I think I've learned quite a bit, but it's
  153.       quite possible that some of the ideas I relate here may have some
  154.       small, or even large, flaws in them.  Nonetheless, I have
  155.       produced a number of working assembly language programs by
  156.       following the ideas presented here.
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                                                                       3
  201.  
  202.       >>THE COMPUTER AS A BIT PATTERN MANIPULATOR.<<
  203.  
  204.       We all have some conception about what a computer does.  On one
  205.       level, it may be thought of as a machine which can execute BASIC
  206.       programs.  Another idea is that the computer is a number
  207.       crunching device.  As I write this primer, I'm using my computer
  208.       as a word processor.
  209.  
  210.       I'd like to introduce a more general concept of just what sort of
  211.       machine a computer is: a bit pattern manipulator.
  212.  
  213.       I'm certain that everyone has been introduced to the idea of a
  214.       *bit*.  (Note: Throughout this primer, a word enclosed in
  215.       *asterisks* is to be read as if it were in italics.)  A bit has
  216.       two states: on and off, typically represented with the symbols
  217.       "1"  and "0".  In this context, DON'T think of 1 and 0 as
  218.       numbers.  They are merely convenient shorthand labels for the
  219.       state of a bit.
  220.  
  221.       The memory of your computer consists of a huge collection of
  222.       bits, each of which could be in either the 1 or 0 (on or off)
  223.       state.
  224.  
  225.       At the heart of your computer is an 8088 microprocessor chip,
  226.       made by Intel.  What this chip can do is manipulate the bits
  227.       which make up the memory of the computer.
  228.  
  229.       The 8088 likes to handle bits in chunks, and so we'll introduce
  230.       special names for the two sizes of bit chunks the 8088 is most
  231.       happy with.  A *byte* will refer to a collection of eight bits.
  232.       A *word* consists of two bytes, or equivalently, sixteen bits.
  233.  
  234.       A collection of bits holds a pattern, determined by the state of
  235.       its individual bits.  Here are some typical byte long patterns:
  236.  
  237.       10101010         11111111         00001111
  238.  
  239.       If you've had a course in probability, it's quite easy to work
  240.       out that there are 256 possible patterns that a byte could hold.
  241.       similarly, a word can hold 65,536 different patterns.
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                                                                       4
  267.  
  268.       All right, now for the single most important idea in assembly
  269.       language programming.  Are you sitting down?  These bit patterns
  270.       can be used to represent other sets of things, by mapping each
  271.       pattern onto a member of the other set.  Doesn't sound like much,
  272.       but IBM has made *BILLIONS* off this idea.
  273.  
  274.       For example, by mapping the patterns a word can hold onto the set
  275.       of integers, you can represent either the numbers from 0 to 65535
  276.       or -32768 to 32767, depending on the exact mapping you use.  You
  277.       might recognize these number ranges as the range of possible line
  278.       numbers, and the possible values of an integer variable, in BASIC
  279.       programs.  This explains these somewhat arbitrary seeming limits:
  280.       BASIC uses words of memory to hold line numbers and integer
  281.       variables.
  282.  
  283.       As another example, you could map the patterns a byte can hold
  284.       onto a series of arbitrarily chosen little pictures which might
  285.       be displayed on a video screen.  If you look in appendix G of
  286.       your BASIC manual, you'll notice that there are *exactly* 256
  287.       different characters that can be displayed on your screen.  Your
  288.       computer uses a byte of memory to tell it what character to
  289.       display at each location of the video screen.
  290.  
  291.       Without getting too far ahead of myself, I'll just casually
  292.       mention that there are about 256 fundamental ways the 8088 can
  293.       manipulate the bit patterns stored in memory.  This suggests
  294.       another mapping which we'll discuss in more detail later.
  295.  
  296.       The point of this discussion is that we can use bit patterns to
  297.       represent anything we want, and by manipulating the patterns in
  298.       different ways, we can produce results which have significance in
  299.       terms of what we're choosing to represent.
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                                                                       5
  333.  
  334.       >>DIGRESSION: A NOTATION SYSTEM FOR BIT PATTERNS<<
  335.  
  336.       Because of their importance, it would be nice to have a
  337.       convenient way to represent the various bit patterns we'll be
  338.       talking about.  We already have one way, by listing the states of
  339.       the individual bits as a series of 1's and 0's.  This system is
  340.       somewhat clumsy, and error prone.  Are the following word
  341.       patterns identical or different?
  342.  
  343.       1111111011111111                         1111111101111111
  344.  
  345.       You probably had trouble telling them apart.  It's easier to tell
  346.       that they're different by breaking them down into more manageable
  347.       pieces, and comparing the pieces.  Here are the same two patterns
  348.       broken down into four bit chunks:
  349.  
  350.       1111 1110 1111 1111                  1111 1111 0111 1111
  351.  
  352.       Some clown has given the name *nybble* to a chunk of 4 bits,
  353.       presumably because 4 bits are half a byte.  A nybble is fairly
  354.       easy to handle.  There are only 16 possible nybble long patterns,
  355.       and most people can distinguish between the patterns quite
  356.       easily.
  357.  
  358.       Each nybble pattern has been given a unique symbol agreed upon by
  359.       computer scientists.  The first 10 patterns were given symbols
  360.       "0" through "9", and when they ran out of digit style symbols,
  361.       they used the letters "A" through "F" for the last six patterns.
  362.       Below is the "nybble pattern code":
  363.  
  364.       0000 = 0    0001 = 1    0010 = 2    0011 = 3
  365.  
  366.       0100 = 4    0101 = 5    0110 = 6    0111 = 7
  367.  
  368.       1000 = 8    1001 = 9    1010 = A    1011 = B
  369.  
  370.       1100 = C    1101 = D    1110 = E    1111 = F
  371.  
  372.       Using the nybble code, we can represent the two similar word
  373.       patterns given above, with the following more manageable
  374.       shorthand versions:
  375.  
  376.                      FEFF       FF7F
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.